package com.example.thread.create.newfour;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class MyThread4 {
    public static void main(String[] args) {
        //1.提供指定线程数量的连接池
        ExecutorService service = Executors.newFixedThreadPool(10);

        ThreadPoolExecutor service1 = (ThreadPoolExecutor)service;
        //设置线程池的属性
        System.out.println(service.getClass());//获取service是在哪个类实现的
        //ctrl+shift+t将获取到的这个类名ThreadPoolExecutor进行搜索

        service1.setCorePoolSize(15);//


        //2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
        service.execute(new NumberThread());//适合使用于Runnable
        service.submit(new NumberThread1());//适合使用于Callable


        //3.关闭连接池
        service.shutdown();


    }
    static class NumberThread implements Runnable{

        @Override
        public void run() {
            for (int i = 1; i <= 100; i++) {
                if(i % 2 ==0){

                    System.out.println(Thread.currentThread().getName()+":"+i);
                }
            }
        }
    }
    static class NumberThread1 implements Callable {
        @Override
        public Object call() throws Exception {
            int sum = 0;
            for (int i = 1; i <=100; i++) {
                if(i % 2 == 0){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                    sum += i;
                }
            }
            return sum;
        }
    }
}
